﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Nissen_Resources;

namespace Nissen_Manage.Forms
{
	public partial class ManageReport : Form
	{
		public ManageReport()
		{
			InitializeComponent();
		}

		private Thread t;
		private void btnReport_Click(object sender, EventArgs e)
		{
			if(this.cbxReportType.SelectedValue.ToString() != "")
			{
				saveFileDialog1.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx";
				if (saveFileDialog1.ShowDialog() == DialogResult.OK)
				{
					string sFilePathName = saveFileDialog1.FileName;

					string reportType = this.cbxReportType.SelectedValue.ToString();
					ControlPage(true);

					t = new Thread(delegate() { ReportData(reportType, sFilePathName);  });
					t.Start();
					
				}
			}
		}

		private void ManageReport_Load(object sender, EventArgs e)
		{
			IList<ComboxModel> comboxModels = new List<ComboxModel>();

			comboxModels.Add(new ComboxModel(){key = "",name = "请选择..."});
			comboxModels.Add(new ComboxModel() { key = "1", name = "每天作业数量" });
			comboxModels.Add(new ComboxModel() { key = "2", name = "小时作业数量" });
			comboxModels.Add(new ComboxModel() { key = "3", name = "用户工作量" });
			comboxModels.Add(new ComboxModel() { key = "4", name = "用户所有错误, 其中语文错误单独统计" });
			comboxModels.Add(new ComboxModel() { key = "5", name = "用户所有工作量, 其中语文单独统计" });
			comboxModels.Add(new ComboxModel() { key = "6", name = "用户校验工作工作量" });
            comboxModels.Add(new ComboxModel() { key = "7", name = "日本側処理" });
            comboxModels.Add(new ComboxModel() { key = "8", name = "GP件数统计" });

			this.cbxReportType.DataSource = comboxModels;
			cbxReportType.ValueMember = "key";
			cbxReportType.DisplayMember = "name";

            this.dateTimePicker_from.Visible = false;
            this.dateTimePicker_to.Visible = false;
		}


		private void ReportData(string type,string savePath)
		{
			try
			{
				ManageReportService manageReportService = new ManageReportService();
				switch (type)
				{
					case "1":
						manageReportService.GetGeresultDay(savePath); break;
					case "2":
						manageReportService.GetGeresultDayHour(savePath); break;
					case "3":
						manageReportService.GetGeresultPerson(savePath); break;
					case "4":
						manageReportService.GetGeresultPersonMissTotalYuwe(savePath); break;
					case "5":
						manageReportService.GetGeresultPersonTotalYuwen(savePath); break;
					case "6":
						manageReportService.GetGeresultPersonSysUpdId(savePath); break;
                    case "7":
                        manageReportService.GetGeresultWithNihonSide(savePath); break;
                    case "8":
                        manageReportService.GetGPCountByDate(savePath,this.dateTimePicker_from.Value,this.dateTimePicker_to.Value); break;
				}
				this.Invoke(new EventHandler(delegate
				{
					this.ShowMessage(ResourceCommon.GetResource("ReportSuccess"));
					ControlPage(false);
				}));
			}
			catch (Exception ex)
			{

				if (Thread.CurrentThread.ThreadState == ThreadState.AbortRequested)
				{
					return ;

				}
				this.Invoke(new EventHandler(delegate
				{
					this.ShowMessage(ex.Message);
					ControlPage(false);
				}));
			}
			

		}
		/// <summary>
		/// 消息提示
		/// </summary>
		/// <param name="msg"></param>
		public void ShowMessage(string msg)
		{
			MessageBox.Show(msg);
		}
		private void ControlPage(bool flag)
		{
			if (flag)
			{
				lblLoading.Text = ResourceCommon.GetResource("ReportLoading");
				btnReport.Enabled = false;
			}
			else
			{
				lblLoading.Text = "";
				btnReport.Enabled = true;
			}
		}
		private void ManageFileImport_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (t != null)
			{
				t.Abort();
			}
		}

        private void cbxReportType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbxReportType.SelectedIndex == 8)
            {
                this.dateTimePicker_from.Visible = true;
                this.dateTimePicker_to.Visible = true;
            }
            else {
                this.dateTimePicker_from.Visible = false;
                this.dateTimePicker_to.Visible = false;
            }
        }
	}
	public class ComboxModel
	{
		public string key { get; set; }

		public string name { get; set; }
	}
}
